<?php

class MenuController extends AdminController {

    public $parentUrl;

    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

    public function actionIndex() {
        try {
            $model = new Menu;
            $model->active = 1;
            $data = $model->findAll();
            $this->render('index', array(
                'data' => $data,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionCreatemenu() {
        try {
            $model = new Menu;
            if (isset($_POST['Menu'])) {
                $model->attributes = $_POST['Menu'];
                $model->validate();
                if (!$model->hasErrors()) {
                    $this->saveMenu($model);
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/menu/index'));
                }
            }
            $this->render('createmenu', array(
                'model' => $model
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionView($id) {
        try {
            $this->render('view', array(
                'model' => $this->loadModel($id),
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdate($id) {
        try {
            $model = $this->loadModel($id);
            if (isset($_POST['Menu'])) {
                $model->attributes = $_POST['Menu'];
                $model->order = $_POST['Menu']['order'];
                $model->active = $_POST['Menu']['active'];
                $model->static = isset($_POST['Menu']['static'])? 1: 0;
                $model->validate();
                if (!$model->hasErrors()) {
                    if ($model->save()) {
                        $this->redirect(array('view', 'id' => $model->id));
                    }
                }
            }
            $this->render('update', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }
    
    public function actionDelete($id){
        try {
            if ($model = $this->loadModel($id)) {
                if ($model->delete()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/menu/index'));
                }
            }
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id) {
        try {
            $model = Menu::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    function saveMenu($model) {
                $menu = new Menu;
                $menu->name = (isset($_POST['Menu']['name'])) ? $_POST['Menu']['name'] : '';
                $menu->active = (isset($_POST['Menu']['active'])) ? $_POST['Menu']['active'] : 0;
                $menu->order = (isset($_POST['Menu']['order'])) ? $_POST['Menu']['order'] : 0;
                $menu->static = (isset($_POST['Menu']['static']) && $_POST['Menu']['static'] == 'on') ? 1 : 0;
                $menu->save();
    }

}

?>
